const filter = {
  //计算流量值（ flowVlueBytes为流量的bytes字节数）
  getFlow(flowVlueBytes, type) {
    let flowKb = parseInt(flowVlueBytes)
    let flowMb = 0
    let flowGb = 0
    if (flowKb >= 1024) {
      flowMb = parseInt(flowKb / 1024);
      flowKb = parseInt(flowKb % 1024);
      if (flowMb >= 1024) {
        flowGb = parseInt(flowMb / 1024);
        flowMb = parseInt(flowMb % 1024);
      }
    }
    let result = "" + parseInt(flowKb) + 'KB'
    if (flowMb > 0) {
      result = "" + parseInt(flowMb) + "MB" + result
    }
    if (flowGb > 0) {
      result = "" + parseInt(flowGb) + "GB" + result
    }
    return result
  },
  //将秒数转换为时分秒格式
  formatSeconds(value) {
    let theTime = parseInt(value);// 秒
    let middle = 0;// 分
    let hour = 0;// 小时
    if (theTime >= 60) {
      middle = parseInt(theTime / 60);
      theTime = parseInt(theTime % 60);
      if (middle >= 60) {
        hour = parseInt(middle / 60);
        middle = parseInt(middle % 60);
      }
    }
    let result = "" + parseInt(theTime)
    if (parseInt(result) < 10) {
      result = '0' + result
    }
    if (middle >= 0) {
      result = "" + parseInt(middle) + ":" + result
      if (parseInt(result) < 10) {
        result = '0' + result
      }
    }
    if (hour >= 0) {
      result = "" + parseInt(hour) + ":" + result
      if (parseInt(result) < 10) {
        result = '0' + result
      }
    }
    return result
  },
  //判断url路径上是否存在某个参数
  getUrlParam(url, key) {
    url = url || location.search;
    var arr = [], obj = {};
    if (key) {
      url.replace(new RegExp("[&?]" + key + "=([^&#]*)", "ig"), function (a, b) {
        arr.push(b);
      });
      return arr.join(",");
    } else {
      url.replace(/[?&]([^&#]+)=([^&#]*)/g, function (a, b, c) {
        obj[b] = c;
      });
      return obj;
    }
  },
  //判断是否是微信浏览器
  isWeiXin() {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
      return true; // 是微信端
    } else {
      return false;
    }
  },
  //16进制转字符串
  hexCharCodeToStr(hexCharCodeStr) {
    var trimedStr = hexCharCodeStr.trim();
    var rawStr =
      trimedStr.substr(0, 2).toLowerCase() === "0x"
        ?
        trimedStr.substr(2)
        :
        trimedStr;
    var len = rawStr.length;
    if (len % 2 !== 0) {
      alert("Illegal Format ASCII Code!");
      return "";
    }
    var curCharCode;
    var resultStr = [];
    for (var i = 0; i < len; i = i + 2) {
      curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
      resultStr.push(String.fromCharCode(curCharCode));
    }
    return resultStr.join("");
  }
}
export default filter